package com.bos.controller;

import com.bos.dao.KabupatenDAO;
import com.bos.dao.KecamatanDAO;
import com.bos.domain.Kabupaten;
import com.bos.domain.Kecamatan;
import com.bos.editor.KabupatenEditor;
import com.bos.editor.KecamatanEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/18/11
 * Time: 3:06 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class KecamatanController {

    private KecamatanDAO kecamatanDAO;
    private KabupatenDAO kabupatenDAO;

/* --------------------- Getter and Setter ---------------------*/

    @Autowired
    public void setKecamatanDAO(KecamatanDAO kecamatanDAO) {
        this.kecamatanDAO = kecamatanDAO;
    }

    public KabupatenDAO getKabupatenDAO() {
        return kabupatenDAO;
    }

    @Autowired
    public void setKabupatenDAO(KabupatenDAO kabupatenDAO) {
        this.kabupatenDAO = kabupatenDAO;
    }

    /* -------------------------- Other Methods -------------------------- */

    @RequestMapping(value = "/kecamatan", method = RequestMethod.GET)
    public String index(ModelMap modelMap) {
        modelMap.addAttribute("data", kecamatanDAO.getAll());

        return "kecamatan/index";
    }

    @RequestMapping(value = "/kecamatan/{id}", method = RequestMethod.GET)
    public String show(ModelMap modelMap, @PathVariable("id") Kecamatan kecamatan) {
        modelMap.addAttribute("data", kecamatan);

        return "kecamatan/show";
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Kabupaten.class, new KabupatenEditor(kabupatenDAO));
        binder.registerCustomEditor(Kecamatan.class, new KecamatanEditor(kecamatanDAO));
    }

}
